<include file="Common/common" />
<?php $statusArr = array('-1'=>"用户撤回",'0'=>'出售中'); ?>

<body>
	
	<div class="layui-tab-item layui-show">
		<div class="layui-main">
			<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			  	<legend style="color: rgb(240,222,112);">挂卖</legend>
			</fieldset>

			<form class="layui-form" id="sell-form" action="">
				<div class="layui-form-item">
					<div class="layui-form-mid layui-word-aux">
						<span style="color: rgb(240,222,112);">
							挂卖数量(可用交易积分:<span id="canusetrading">{$userInfo['score_trading']}</span>)</span>
						<input style="background-color: rgba(255,255,255,0.2); color: rgb(240,222,112);" type="number" id="sell" name="sell" lay-verify="required" min="{$scope['min']}" max="{$scope['max']}" onkeyup="checknum(this);" autocomplete="off"  class="layui-input">
						<div class="layui-form-mid layui-word-aux">
							<span style="color: rgb(240,222,112);">
							挂卖数量需要在{$scope['min']}到{$scope['max']}之间
							</span>
						</div>

					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block" style="margin-left: 0px;">
						<button  style="color: rgb(240,222,112);background: rgb(57,61,73);font-size: 16px"  class="layui-btn" lay-submit="" lay-filter="btn-sell">确定</button>
					</div>
				</div>
			</form>
			<br>

			<div style="overflow-x: auto;">
				<table class="layui-table" id="sell-list" style="color: rgb(240,222,112);">
					<thead>
							<tr style="background: rgb(57,61,73)">
								<th>编号</th>
								<th>出售积分</th>
								<th>出售用户</th>
								<th>出售时间</th>
								<th>状态</th>
								<th>操作</th>
							</tr> 
						</thead>
						<tbody>
							<foreach name="sellList" item="vo">
								<tr style="background: #7d7d7d;color:rgb(240,222,112);">
									<td>{$vo['id']}</td>
									<td>{$vo['num']}</td>
									<td>{$vo['sell_account']}</td>
									<td><?php echo date('Y-m-d H:i:s',$vo['createtime']) ?></td>
									<td><if condition="$vo['status'] gt 1">已经出售<else />{$statusArr[$vo['status']]}</if></td>
									<td><if condition="$vo['status'] eq 0"><button style="color: rgb(240,222,112);background: rgb(57,61,73);font-size: 16px" class="layui-btn" onclick="func_cancel(this,{$vo['id']})">撤回</button></if></td>
								</tr>
							</foreach>
						</tbody>
				</table>
			</div>
			<div class="green-black">{$page}</div>

		</div>
	</div>
</body>
<script type="text/javascript">
	var min_num = {$scope['min']};
	var max_num = {$scope['max']};
	var account = "{$userInfo['account']}";
	// 验证数字
   	function checknum(obj){   
     	if(/^\d+\.?\d{0,2}$/.test(obj.value)){
        	obj.value = obj.value;
     	}else{
        	obj.value = obj.value.substring(0,obj.value.length-1);
     	}     
   	}

   	// 取消挂卖
   	function func_cancel(obj,id){
   		layer.confirm('确定要撤回？撤回后积分将转回到交易积分！',{offset:'none'},function(index){
   			$.ajax({
	            type: "post",
	            url: "{:U('Order/ctrl_cancel')}",
	            data: {id:id},
	            dataType: "json",
	            success: function(rtJson){
                    layer.msg('<span style="color: rgb(240,222,112);">'+rtJson.msg+'</span>');
	            	setTimeout(function(){location.reload();}, 1000);
	            }
	        });
   		});
   		
   	}

   	layui.use(['form','layer','jquery'], function () {
     
        // 操作对象
        var form = layui.form;
        var $ = layui.jquery;

        //挂卖
        form.on('submit(btn-sell)',function (data) {
        	var canusetrading = parseFloat($("#canusetrading").text());
        	var sell = parseFloat(data.field.sell.trim());
        	if (canusetrading < sell) {
                layer.msg('<span style="color: rgb(240,222,112);">交易积分不足，无法挂卖</span>');
        		return false;
        	}else{
        		layer.confirm('确定要挂卖？',{offset:'none'},function(index){
        			$.ajax({
			            type: "post",
			            url: "{:U('Order/ctrl_sell')}",
			            data: data.field,
			            dataType: "json",
			            success: function(rtJson){
			            	if (rtJson.code == 1) {
			            		// 修改挂卖参数
			            		$("#canusetrading").text((canusetrading - sell).toFixed(2) );

			            		var innerTD = "";
			            		innerTD += "<tr style=\"background: #7d7d7d;color:rgb(240,222,112);\"><td>"+rtJson.data+"</td>"+
			            				"<td>"+sell+"</td>"+
			            				"<td>"+account+"</td>"+
			            				"<td>"+rtJson.date+"</td>"+
			            				"<td>出售中</td>"+
			            				"<td><button style=\"color: rgb(240,222,112);background: rgb(57,61,73);font-size: 16px\"   class='layui-btn'  onclick='func_cancel(this,"+rtJson.data+")'>撤回</button></td></tr>";

			            		$(innerTD).prependTo('#sell-list');
			            		$('#sell-form')[0].reset();

                                layer.msg('<span style="color: rgb(240,222,112);">'+rtJson.msg+'</span>');
			            	}else{
                                layer.msg('<span style="color: rgb(240,222,112);">'+rtJson.msg+'</span>');
			            	}
			               	
			            }
			        });
        		});
        	}
        	return false;
        });
    });
</script>
<include file="Common/footer" />